# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################


import uuid
from flask import Blueprint
from flask import request
import utils.pgsql.pgpoint as point
from utils.wraps import token, urlcost
import utils.common.initres as initres
import utils.message.response as response
import utils.common.func as func
import json
import utils.pgsql.pgadmin

### point data configuration management


point_module = Blueprint("base_point", __name__, url_prefix="/api/v1/caseconf/point")


@point_module.route("/admin/<int:adminId>", methods=["GET"])
@token.login_required
@urlcost.cost_count_wraps
def point_all_admin(adminId):

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    obj = point.PgPoint()
   
    if userInfo["role"] == 1:
        return response.make_response_success(obj.allByAdminRole1(adminId))
    elif userInfo["role"] == 2:
        operatorId = userInfo["operatorId"]
        return response.make_response_success(obj.allByAdminRole2(operatorId))
    elif userInfo["role"] == 3:
        obj_admin = utils.pgsql.pgadmin.PgAdmin()
        caseId = obj_admin.get_caseId(adminId)["caseId"]
        return response.make_response_success(obj.allByAdminRole3(caseId))
    else:
        return response.make_response_401()


@point_module.route("/case/<int:caseId>", methods=["GET"])
@token.login_required
@urlcost.cost_count_wraps
def point_all_case(caseId):

    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] in [1, 2, 3]:
        obj = point.PgPoint()
        return response.make_response_success(obj.allByCase(caseId))
    else:
        return response.make_response_401()


@point_module.route("/", methods=["POST"])
@token.login_required
@urlcost.cost_count_wraps
def point_add():
  
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] in [1,2,3]:
        obj = point.PgPoint()
        dic = json.loads(request.data)

        if (
            obj.get_pointName(dic.get("pointName"), dic.get("caseId")).get("pointName")
            is not None
        ):
            return response.make_response_1505()

        res = obj.addPoint(dic)
        return response.make_response_success(res)
    else:
        return response.make_response_401()


@point_module.route("/<int:pointId>", methods=["GET"])
@token.login_required
@urlcost.cost_count_wraps
def point_get(pointId):
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] in [1, 2, 3]:
        obj = point.PgPoint()
        return response.make_response_success(obj.get(pointId))
    else:
        return response.make_response_401()


@point_module.route("/<int:pointId>", methods=["PUT"])
@token.login_required
@urlcost.cost_count_wraps
def point_put(pointId):
   
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo["role"] in [1, 2, 3]:
        obj = point.PgPoint()
        dic = json.loads(request.data)
        if (
            obj.get_pointeditName(dic.get("pointName"), dic.get("caseId"), pointId).get(
                "pointName"
            )
            is not None
        ):
            return response.make_response_1505()

        if obj.modify(pointId, dic):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()


@point_module.route("/<int:pointId>", methods=["DELETE"])
@token.login_required
@urlcost.cost_count_wraps
def point_delete(pointId):
   
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo["role"] in [1,2, 3]:
        obj = point.PgPoint()
        if obj.delete(pointId):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()

@point_module.route("/pointall", methods=["GET"])
@urlcost.cost_count_wraps
def point_all():
  
    obj = point.PgPoint()
    res = obj.all()
    if(res):
        return response.make_response_success(res)
    else:
        return response.make_response_1000()

 
@point_module.route("/case/pointall/<int:caseId>", methods=["GET"])
@urlcost.cost_count_wraps
def point_case_all(caseId):

    obj = point.PgPoint()
    res =  obj.allByCase(caseId)
    if(res):
        return response.make_response_success(res)
    else:
        return response.make_response_1000()